package com.gxa.controller;

import com.gxa.pojo.CompanyInfo;
import com.gxa.service.CompanyInfoService;
import com.gxa.service.LogService;
import com.gxa.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/company")
@CrossOrigin
public class CompanyInfoController {

    @Autowired
    CompanyInfoService companyInfoService;

    @Autowired
    LogService logService;

    JsonResult jsonResult;

    @ResponseBody
    @RequestMapping("/insertCompanyInfo")
    public Map insertCompanyInfo(@RequestBody Map map){
        jsonResult=new JsonResult();
        int i = companyInfoService.insertCompanyInfo(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/companyEdit")
    public Map updateCompanyInfo(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        int i = companyInfoService.updateCompanyInfo(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/companyInfo")
    public List<CompanyInfo> findCompanyInfo(){
        jsonResult=new JsonResult();
        List<CompanyInfo> company = companyInfoService.findCompany();
        return company;
    }

}